<?php

namespace App\Console\Commands\Push;

use App\Models\System\SystemNotifyCenterModel;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

class FixMsgCenterPushTypeCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'push:fixMsgCenterPushTypeCommand';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '推送：消息中心推送类型批量修复';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle(): int
    {
        SystemNotifyCenterModel::query()->where("pushType", 0)->chunkById(200, function (Collection $collection) {
            $collection->each(function (SystemNotifyCenterModel $systemNotifyCenterModel) {
                if ($systemNotifyCenterModel->url) {
                    $systemNotifyCenterModel->pushType = SystemNotifyCenterModel::PUSH_TYPE_2;
                } else {
                    $systemNotifyCenterModel->pushType = SystemNotifyCenterModel::PUSH_TYPE_3;
                }
                if ($systemNotifyCenterModel->pushType > 0) {
                    $systemNotifyCenterModel->save();
                } else {
                    $this->error($systemNotifyCenterModel->id);
                }
            });
        });
        return 0;
    }
}
